#include "stack.h"

void Init(Stack* st)
{
	assert(st);

	st->data = NULL;
	st->top = 0;
	st->capacity = 0;
}

void Push(Stack* st, DataType x)
{
	assert(st);

	if (st->capacity == st->top)
	{
		int newcapacity = (st->capacity == 0) ? 4 : st->capacity * 2;

		DataType* temp = (DataType*)realloc(st->data, sizeof(DataType) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		st->data = temp;
		st->capacity = newcapacity;
	}

	st->data[st->top++] = x;
}

void Pop(Stack* st)
{
	assert(st);
	assert(st->top > 0);

	st->top--;
}

DataType GetTop(Stack* st)
{
	assert(st);
	assert(st->top > 0);

	return st->data[st->top - 1];
}

bool Empty(Stack* st)
{
	assert(st);

	return (st->top == 0);
}

void Destroy(Stack* st)
{
	assert(st);

	free(st->data);
	st->data = NULL;
	st->top = st->capacity = 0;

}

int Size(Stack* st)
{
	assert(st);
	
	return st->top;
}